package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.SystemMessage;
import cn.com.connext.scrm.repository.master.SystemMessageRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.SystemMessageDTO;
import cn.com.connext.scrm.service.mapper.SystemMessageMapper;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.vm.PageQueryVM;
import cn.com.connext.scrm.web.rest.vm.SystemMessageVM;
import com.connext.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.ZonedDateTime;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-03 10:42
 **/
@Service
@Transactional
public class SystemMessageService extends BaseService{

    private final Logger log = LoggerFactory.getLogger(SystemMessageService.class);

    @Autowired
    private SystemMessageRepository systemMessageRepository;

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    @Autowired
    private SystemMessageUserLogService systemMessageUserLogService;


    protected SystemMessageService(SystemMessageRepository repository) {
        super(repository);
        this.systemMessageRepository = repository;
    }


    public SystemMessageDTO edit(SystemMessageVM vm) {
        SystemMessage message;
        if (StringUtils.isNotEmpty(vm.getId())){
            message = systemMessageRepository.findOne(vm.getId());
            if (message.getSend()){
                throw new ServiceException(MyErrorCode.ERROR_MESSAGE_ISSEND);
            }
        }else {
            message = new SystemMessage();
            message.setRead(false);
            message.setSend(false);
            message.setCreateTime(ZonedDateTime.now());
        }
        if (StringUtils.isNotEmpty(vm.getContent())) {
            message.setContent(vm.getContent());
        }
        message.setType(vm.getType());
        if (StringUtils.isNotEmpty(vm.getUrl())) {
            message.setUrl(vm.getUrl());
        }
        if (vm.getShow() != null){
            message.setShow(vm.getShow());
        }
        if (StringUtils.isNotEmpty(vm.getRedirectType())){
            message.setRedirectType(vm.getRedirectType());
        }
        if (StringUtils.isNotEmpty(vm.getRedirectH5Url())){
            message.setRedirectH5Url(vm.getRedirectH5Url());
        }
        if (StringUtils.isNotEmpty(vm.getMiniProgranPath())){
            message.setMiniProgranPath(vm.getMiniProgranPath());
        }
        if (StringUtils.isNotEmpty(vm.getMiniProgranAppid())){
            message.setMiniProgranAppid(vm.getMiniProgranAppid());
        }
        message.setUpdateTime(ZonedDateTime.now());
        SystemMessage result = systemMessageRepository.save(message);
        SystemMessageDTO dto = systemMessageMapper.toDto(result);
        if (StringUtils.isNotEmpty(vm.getUserId())){
            systemMessageUserLogService.saveLog(message.getId(), vm.getUserId(), message.getShow());
        }else {
            systemMessageUserLogService.saveAllLog(message.getId(), message.getShow());
        }

        return dto;
    }

    public SystemMessageDTO send(String id) {
        SystemMessage message = systemMessageRepository.findOne(id);
        SystemMessageDTO dto = null;
        if (message == null){
            return null;
        }
        if (message.getSend()){
            return null;
        }
        message.setSend(true);
        message.setUpdateTime(ZonedDateTime.now());
        SystemMessage result = systemMessageRepository.save(message);
        dto = systemMessageMapper.toDto(result);
        systemMessageUserLogService.send(id);
        return dto;
    }

    public SystemMessageDTO findById(String id) {
        SystemMessage message = systemMessageRepository.findOne(id);
        if (message!= null){
            SystemMessageDTO dto = systemMessageMapper.toDto(message);
            return dto;
        }
        return null;
    }

    public Page<SystemMessage> pageQuery(PageQueryVM vm) {
        Page<SystemMessage> page = systemMessageRepository.findAll(new PageRequest(vm.getPage() - 1, vm.getPageSize(), Sort.Direction.DESC, "createTime"));
        return page;
    }

    public JsonResult deleteById(String id) {
        JsonResult result = new JsonResult();
        SystemMessage message = systemMessageRepository.findOne(id);
        if (message == null){
            result.setState(1);
            result.setData("数据不存在");
            return result;
        }
        if (message.getSend()){
            result.setState(1);
            result.setData("消息已经发送了");
            return result;
        }
        systemMessageRepository.delete(id);
        systemMessageUserLogService.deleteByMessageId(id);
        result.setMessage("删除成功");
        result.setState(0);
        return result;
    }
}
